-- 状态机
-- 8个led等，在四种状态循环执行，
-- s0:左到右依次点亮 s1:全部熄灭 s2:右到左依次点亮 s3:全部熄灭


library ieee;
use ieee.std_logic_1164.all;

entity light of
    port(
      clk : in std_logic;
      rst : int std_logic;
      q   : out std_logic_vector(7 downto 0)
    );
end light;

architecture behave of light is
    type Status is(s0, s1, s2, s3);
    signal state : Status;
    signal q1 : std_logic_vector(7 downto 0);
    signal count : std_logic_vector(3 downto 0);
begin

    -- 状态转换进程
    change_status: process(clk, rst) begin
        if rst = '1' then
            state <= s0;
        elsif clk'event and clk = '1' then
            case state is
                when s0 => if count = "0111" then
                                state = s1;
                            end if;
                when s1 => state <= s2;
                when s2 => if count = "0111" then
                                state = s3;
                            end if;
                when s3 => state => s0;
            end case;           
        end if;
    end process;

    -- 输出进程
    output: process(clk, rst) begin
        if rst = '1' then
            q1 <= (others => '0');
            count <= "0000";
        elsif clk'event and clk = '1' then
            case state is
                when s0 => q1 <= '1' & q1(7 downto 1);  -- 移位寄存器，依次点亮led灯（&为连接符号）
                            if count = "0111" then
                                count <= (others => '0');
                            else
                                count <= count + 1;
                            end if;
                when s1 => q1 <= (others => '0');
                when s2 => q1 <= q1(6 downto 0) & '1';
                            if count = "0111" then
                                count <= (others => '0');
                            else
                                count <= count + 1;
                            end if;
                when s3 => q1 <= (others => '0');
            end case;
        end if;
    end process;

    q <= q1; -- 输出

end behave;